<?php

namespace app\api\controller;

use app\admin\model\Shopstaff;
use app\admin\model\Useragentwechat;
use app\admin\model\Userweixin;
use app\admin\model\Userweixinmp;
use app\admin\model\Userweixinmsg;
use app\common\controller\Api;
use app\common\controller\Frontend;
use app\common\model\WechatAutoreply;
use app\common\model\WechatContext;
use app\common\model\WechatResponse;
use app\common\model\WechatConfig;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use addons\wechat\library\Wechat as WechatService;
use addons\wechat\library\Config as ConfigService;
use fast\Random;
use think\Db;
use think\Exception;
use think\Hook;
use think\Log;

/**
 * 微信授权
 */
class Wechat extends Api
{
    public $app = null;

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->app = Factory::officialAccount(ConfigService::load());
    }

    public function api()
    {
        $this->app->server->push(function ($message) {


            error_log("===message==".json_encode($message));
            switch ($message['MsgType']) {
                case 'event':

                    if($message['EventKey'] =='662718c7613bf'){
                        return "请输入您要绑定的手机号账号";
                    }

                    break;
                case 'text':
                    $content = $message['Content'];
                    $pattern = "/^1[3456789]{1}\d{9}$/";
                    $isPhone = preg_match($pattern,$content);

                    $openid = $message['FromUserName'];
                    if($isPhone){

                        $userWeixinMpModel = new Userweixinmp();
                        $userModel = new \app\admin\model\User();
                        $user = $userModel->where('mobile',$content)->find();
                        if(!$user){

                            return "此手机号暂未注册 无法绑定";
                        }
                        $wx = $userWeixinMpModel->where('user_id',$user['id'])->find();
                        error_log("==wx==".json_encode($wx));
                        if($wx){
                            return "您已绑定，无需再绑定";
                        }

                        error_log($content."===wx insert==".$openid);
                        $userWeixinMpModel->insert(['user_id'=>$user['id'],'openid'=>$openid]);
                        return "绑定成功";

                    }else{
                        return "如有加盟问题，请咨询专属电话 4008885626";
                    }

                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });
        $response = $this->app->server->serve();
        $response->send();

    }


    /**
     * 登录授权
     */
    public function oauth()
    {

        $mobile = $this->request->param('mobile');
        error_log("=====oauth===".$mobile);
        session("mobile",$mobile);
        $response = $this->app->oauth->scopes(['snsapi_base'])
            ->redirect();
        $response->send();

    }


    /**
     * 授权回调
     */
    public function callback()
    {
        $user = $this->app->oauth->user();
        $id = $user->getId();


        exit();

    }


}
